package comm.hg.web;

import com.google.gson.Gson;
import comm.hg.bean.FaPiaoLei;
import comm.hg.bean.GongYingShangLei;
import comm.hg.bean.KuCunLei;
import comm.hg.bean.ShangPinLei;
import comm.hg.db.MySQL_HuaGong;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

//配置servlet映射url
@WebServlet(urlPatterns = "/se")
//上传图片注解
@MultipartConfig
public class HuaGong extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到一个参数
        String type=req.getParameter("type");
        if("1".equals(type)){
            //查询所有
            try {
                //调用查询
                List<GongYingShangLei> arr= MySQL_HuaGong.select();
                req.getSession().setAttribute("arr",arr);
                //页面转发
                req.getRequestDispatcher("/view/gongYingShang.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("2".equals(type)){
            //通过供应商或者业务员查询 信息
            String name=req.getParameter("name");
            String uid=req.getParameter("uid");
            try {
              List<GongYingShangLei> arr=MySQL_HuaGong.selectByid(name,uid);
              req.getSession().setAttribute("arr",arr);
              //重定向
              //resp.sendRedirect("/muLu/gongYingShang.jsp");
              //页面转发
              req.getRequestDispatcher("/view/gongYingShang.jsp").forward(req, resp);


            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("3".equals(type)){
            //添加供应商信息
            Date date=new Date();
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String create_time=s.format(date);
            String name=req.getParameter("name");
            String phone=req.getParameter("phone");
            String wechat=req.getParameter("wechat");
            //String create_time=req.getParameter("create_time");
            String remark=req.getParameter("remark");
            String uid=req.getSession().getAttribute("uid").toString();
            String sql="insert into supplier(name,phone,wechat,create_time,remark,user_id,status) values(?,?,?,?,?,?,?)";
            try {
                //调用添加
                MySQL_HuaGong.updateShuJu(sql,name,phone,wechat,create_time,remark,uid,"0");
                //调用查询
                List<GongYingShangLei> arr= MySQL_HuaGong.select();
                req.getSession().setAttribute("arr",arr);
                //页面转发
                req.getRequestDispatcher("/view/gongYingShang.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("4".equals(type)){
            //修改供应商信息
            Date date=new Date();
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String create_time=s.format(date);
            String name=req.getParameter("name");
            String phone=req.getParameter("phone");
            String wechat=req.getParameter("wechat");
            //String create_time=req.getParameter("create_time");
            String remark=req.getParameter("remark");
            String gid=req.getParameter("gid");
            String sql="update supplier set name=?,phone=?,wechat=?,remark=? where gid=?";
            try {
                //调用修改语句
                MySQL_HuaGong.updateShuJu(sql,name,phone,wechat,remark,gid);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("5".equals(type)){
            //删除供应商信息
            String gid=req.getParameter("gid");
            String sql="delete from supplier where gid=?";
            try {
                //调用删除语句
                MySQL_HuaGong.updateShuJu(sql,gid);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("6".equals(type)){
            //根据id查询
            String gid=req.getParameter("gid");
            try {
                //调用
                GongYingShangLei  arr=MySQL_HuaGong.selectId(gid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }


        }else if("7".equals(type)){
            //商品管理查询
            try {
                List<ShangPinLei> arr=MySQL_HuaGong.shangPingSelect();
                req.getSession().setAttribute("arr",arr);
                //req.getRequestDispatcher("/muLu/ShangPing.jsp").forward(req,resp);
                //重定向
               resp.sendRedirect("/view/ShangPing.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("8".equals(type)){
            //商品管理模糊查询
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            String riQi=req.getParameter("riQi");
            String riQi1=req.getParameter("riQi1");
            try {
                List<ShangPinLei> arr=MySQL_HuaGong.shangPinSelectMuHu(name,cas,riQi,riQi1);
                req.getSession().setAttribute("arr",arr);
                //重定向
                resp.sendRedirect("/view/ShangPing.jsp");

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("9".equals(type)){
            //商品查询供应商名称
            try {
                String name=req.getParameter("gongYingShang");
                List<ShangPinLei> arr=MySQL_HuaGong.shangPinSelectName(name);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("10".equals(type)){
            //商品查询供应商名称
            try {
                String name=req.getParameter("gongYingShang");
                List<ShangPinLei> arr=MySQL_HuaGong.shangPinSelectName1();
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("11".equals(type)){
            //添加商品信息
            Date date=new Date();  //时间
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String create_time=s.format(date);
            String update_time=s.format(date);
            String name=req.getParameter("name");//商品名称
            String cas=req.getParameter("cas");  //商品cas号
            String price_info=req.getParameter("price_info");//报价信息
            String commodity_info=req.getParameter("commodity_info");//商品介绍
            String supplier_id=req.getParameter("supplier_id");//供应商id
            String uid=req.getSession().getAttribute("uid").toString();

            try {
                ShangPinLei idcas=MySQL_HuaGong.SHangPingSelectCasId(cas,supplier_id);

                if (idcas!=null){
                    resp.getWriter().write("0");
                    return;
                }else if (idcas==null){
                    resp.getWriter().write("1");
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

            String sql1="update supplier set `status`='1' where gid=?";
            String sql="insert into commodity(name,cas,price_info,commodity_info,supplier_id,create_time,update_time,user_id,img_status,file_status) values(?,?,?,?,?,?,?,?,?,?)";
            try {
                MySQL_HuaGong.updateShuJu(sql,name,cas,price_info,commodity_info,supplier_id,create_time,update_time,uid,"0","0");
                //values('橘子','003','1',20,'襄阳橘子','1','2020-12-10')
                MySQL_HuaGong.updateShuJu(sql1,supplier_id);

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else if("12".equals(type)){
            //商品通过id查询姓名电话
            String gid=req.getParameter("gid");//供应商id
            try {
                ShangPinLei arr=MySQL_HuaGong.SHangPingSelectId(gid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("13".equals(type)){
            //修改商品信息
//            String imgFile="http://localhost:8080/img/";
//            //用于获取请求中指定的name文件
//            Part part=req.getPart("img");
//            //获取文件名字
//            String fileNmae=part.getSubmittedFileName();
//            //获取文件名后缀名  截取“.”最后出现的地方
//            fileNmae=fileNmae.substring(fileNmae.lastIndexOf("."));
//            //设置唯一文件名
//            fileNmae= UUID.randomUUID().toString()+fileNmae;
//            //获取当前项目中 存在图片文件夹路径 完整路径
//            String img=this.getServletContext().getRealPath("/img");
//            //写入  文件夹  加  文件名称
//            part.write(img+"\\"+fileNmae);


            String imgFile="http://localhost:8080/img/";
            //用于获取请求中指定的name文件
            Part part=req.getPart("img");
            Part part1=req.getPart("img1");
            String img_path=shangChuang(part);
            String file_path=shangChuang(part1);

            Date date=new Date();  //时间
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String update_time=s.format(date);
            String sid=req.getParameter("sid");//供应商id
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            String price_info=req.getParameter("price_info");
            String commodity_info=req.getParameter("commodity_info");
            String sql="update commodity set name=?,cas=?,price_info=?,commodity_info=?,img_path=?,img_status=?,file_status=?,update_time=?,file_path=? where sid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,name,cas,price_info,commodity_info,img_path,"1","1",update_time,file_path,sid);
                //调用查询
//                List<ShangPinLei> arr=MySQL_HuaGong.shangPingSelect();
//                arr=arr.stream().map(a->{
//                    System.out.println(a.getImg_path());
//                    a.setImg_path(imgFile+a.getImg_path());
//                    return a;
//
//                }).collect(Collectors.toList());
//
//                //设置属性 把值放进去
//                req.getSession().setAttribute("arr", arr);
                //重定向
                List<ShangPinLei> arr=MySQL_HuaGong.shangPinSelectMuHu(null,null,null,null);
                req.getSession().setAttribute("arr",arr);
                //重定向
                resp.sendRedirect("/view/ShangPing.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("14".equals(type)){
            //查询id给修改文本框放值
            String sid=req.getParameter("sid");//供应商id
            try {
                ShangPinLei arr=MySQL_HuaGong.SHangPingSXIuGaiWendyBenFuZhi(sid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("15".equals(type)){
            //通过查询id给查看供应商文本框放值
            String sid=req.getParameter("sid");//供应商id
            try {
                ShangPinLei arr=MySQL_HuaGong.SHangPingChaKanGongYingShang(sid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else if("16".equals(type)){
            //删除供应商信息
            String sid=req.getParameter("sid");
            String sql="delete from commodity where sid=?";
            try {
                //调用删除语句
                MySQL_HuaGong.updateShuJu(sql,sid);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("17".equals(type)){
            //div放图片
            String sid=req.getParameter("sid");//供应商id
            try {
                ShangPinLei arr=MySQL_HuaGong.SHangPingDivTuPian(sid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("18".equals(type)){
            //下载图片
            String path=req.getParameter("img");
            //设置图片路径
//            String path="C:\\Users\\A\\Desktop\\12.jpg";
            //String fileName1="12jpg";
            //找到最后一个
            int a=path.lastIndexOf("/");
            //截取图片名称
            String fileName=path.substring(a+1);

            //中文编码
            fileName= URLEncoder.encode(fileName);

            //弹出下载对话框  attachmentWWI以附件方式下载                                                                            文件名
            resp.setHeader("Content-Disposition", "attachment;fileName="+fileName);
            path=req.getServletContext().getRealPath(path);
            //得到文件对象
            File file=new File(path);

            //得到输入流
            FileInputStream fi=new FileInputStream(file);

            //响应得到输出流
            OutputStream out=resp.getOutputStream();

            byte[] b=new byte[1024];
            int a1;
            while((a1=fi.read(b))!=-1){
                out.write(b,0,a1);
            }
            //关闭流
            out.close();
            fi.close();
        }else if("19".equals(type)){
            //div放文件
            String sid=req.getParameter("sid");//供应商id
            try {
            ShangPinLei arr = MySQL_HuaGong.SHangPingDivWenJian(sid);
            resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else if("20".equals(type)){
            //下载图片
            String path=req.getParameter("img");
            //找到最后一个
            int a=path.lastIndexOf("/");
            //截取图片名称
            String fileName=path.substring(a+1);
            //中文编码
            fileName= URLEncoder.encode(fileName);
            //弹出下载对话框  attachmentWWI以附件方式下载                                                                            文件名
            resp.setHeader("Content-Disposition", "attachment;fileName="+fileName);
            //查看一个上下文对象            返回文件的绝对路径
            path=req.getServletContext().getRealPath(path);
            //得到文件对象
            File file=new File(path);
            //得到输入流
            FileInputStream fi=new FileInputStream(file);
            //响应得到输出流
            OutputStream out=resp.getOutputStream();

            byte[] b=new byte[1024];
            int a1;
            while((a1=fi.read(b))!=-1){
                out.write(b,0,a1);
            }
            //关闭流
            out.close();
            fi.close();
        }else if("21".equals(type)){
            //查询库存表 和模糊
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            String number=req.getParameter("number");
            try {
                List<KuCunLei> arr=MySQL_HuaGong.selectKuCun(name,cas,number);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("22".equals(type)){
            //库存 0查询
            try {
                List<KuCunLei> arr=MySQL_HuaGong.selectKuCunLing();
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("23".equals(type)){
            //库存  添加
            Date date=new Date();  //时间
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String update_time=s.format(date);
            String create_time=s.format(date);
            String number=req.getParameter("number");
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            String amount=req.getParameter("amount");
            String remark=req.getParameter("remark");
            String sql="insert into inventory values(null,?,?,?,?,?,?,?)";
            try {
                MySQL_HuaGong.updateShuJu(sql,number,name,cas,create_time,update_time,amount,remark);
                //(null,1006,'西红柿','006','2020-12-18','2020-12-18',20,'都处理')
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("24".equals(type)){
            //库存  修改
            Date date=new Date();  //时间
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String update_time=s.format(date);
            String create_time=s.format(date);
            String kid=req.getParameter("kid");
            String number=req.getParameter("number");
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            String amount=req.getParameter("amount");
            String remark=req.getParameter("remark");
            String sql="update inventory set number=?,name=?,cas=?,amount=?,remark=?,create_time=? ,update_time=? where kid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,number,name,cas,amount,remark,create_time,update_time,kid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("25".equals(type)){
            //库存 修改文本框放值
            String kid=req.getParameter("kid");
            try {
                KuCunLei arr=MySQL_HuaGong.kuCunXiuGaiId(kid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("26".equals(type)){
            //库存 删除
            String kid=req.getParameter("kid");
            String sql="delete from inventory where kid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,kid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("27".equals(type)){
            //库存 增加数量
            String kid=req.getParameter("kid");
            String amount=req.getParameter("amount");
            String sql="update inventory set amount=amount+? where kid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,amount,kid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("28".equals(type)){
            //库存 减少数量
            String kid=req.getParameter("kid");
            String amount=req.getParameter("amount");
            String sql="update inventory set amount=amount-? where kid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,amount,kid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("29".equals(type)){
            //发票查询  模糊查询
            String name=req.getParameter("name");
            String cas=req.getParameter("cas");
            try {
                List<FaPiaoLei> arr=MySQL_HuaGong.selectFaPiao(cas,name);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("30".equals(type)) {
            //库存  添加
            Date date = new Date();  //时间
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            String create_time = s.format(date);
            String name = req.getParameter("name");
            String cas = req.getParameter("cas");
            String number = req.getParameter("number");
            String price = req.getParameter("price");
            String unit = req.getParameter("unit");
            String sql = "insert into invoice values(null,?,?,?,?,?,?)";
            try {
                MySQL_HuaGong.updateShuJu(sql,name, cas,number,price,create_time,unit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("31".equals(type)) {
            //发票修改
            Date date = new Date();  //时间
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            String create_time = s.format(date);
            String name = req.getParameter("name");
            String cas = req.getParameter("cas");
            String number = req.getParameter("number");
            String price = req.getParameter("price");
            String unit = req.getParameter("unit");
            String sid=req.getParameter("sid");
            String sql = "update invoice set name=?,cas=?,number=?,price=?,create_time=?,unit=? where sid=?";

            try {
                MySQL_HuaGong.updateShuJu(sql,name,cas,number,price,create_time,unit,sid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("32".equals(type)) {
            //发票 修改赋值
            String sid=req.getParameter("sid");
            try {
                FaPiaoLei arr=MySQL_HuaGong.faPiaoXiuGaiFuZhi(sid);
                resp.getWriter().write(new Gson().toJson(arr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("33".equals(type)) {
            //发票删除
            String sid=req.getParameter("sid");
            String sql="delete from invoice where sid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,sid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if("34".equals(type)) {
            String sid=req.getParameter("sid");
            String number=req.getParameter("number");
            String sql="update invoice set number=number+? where sid=?";
            try {
                MySQL_HuaGong.updateShuJu(sql,number,sid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    /**
     * 上传图片文件方法
     */
    public  String shangChuang(Part part) throws IOException {
        //获取文件名字
        String fileNmae=part.getSubmittedFileName();
        //获取文件名后缀名  截取“.”最后出现的地方
        fileNmae=fileNmae.substring(fileNmae.lastIndexOf("."));
        //设置唯一文件名
        fileNmae= UUID.randomUUID().toString()+fileNmae;
        //获取当前项目中 存在图片文件夹路径 完整路径
        String img=this.getServletContext().getRealPath("/img");
        //写入  文件夹  加  文件名称
        part.write(img+"\\"+fileNmae);

        return fileNmae;
    }
}
